$(function () {
    var result = Model.user.getUserInfo();

    $('#usernickname').val(result.loginedUser.nickname);
    $('#item-school').text(result.loginedUser.school);
    $("#register-sub-btn").click(function () {
        if (checknickname() && checkpasswd()) {
            // // 获取默认地址
            var loading = weui.loading('loading', {
                className: 'custom-classname'
            });
            var nickname = $('#usernickname').val();
            var passwd = $('#passwd').val();
            var school = $("#item-school").text();
            var result2 = Model.user.updateUserInfo({
                nickname: nickname,
                passwd: passwd,
                school: school
            });
            if (result2.code == 1) {
                loading.hide();
                weui.toast('修改成功', {
                    duration: 2000,
                    className: 'custom-classname',
                    callback: function () {
                        location.reload();
                    }
                });

            } else {
                loading.hide();
                weui.toast('修改失败，系统错误', {
                    duration: 1500,
                    className: 'custom-classname',
                    callback: function () {
                        // location.reload()
                    }
                });
            }
        }

    });

    function checknickname() {
        //判断数据库中是否存在
        var nickname = $('#usernickname').val();
        var result = Model.user.validateNickname({
            nickname: nickname,
        });
        if (result.code == 0) {
            weui.alert("用户名已存在");
            return false;
        }
        var nickname = $('#usernickname').val();
        if (nickname == null || nickname == "") {
            // 输入不合法
            weui.alert('账户名不能为空');
            return false;
        }
        var reg = /^\w{3,10}$/;
        if (!reg.test(nickname)) {
            weui.alert("请输入3-10个字母或数字或下划线");
            return false;
        }
        $('#usernickname').empty();
        return true;
    }

//判断密码是否符合要求
    function checkpasswd() {
        var passwd = $('#passwd').val();
        var confirm = $('#confirm-passwd').val();
        if (passwd == null || passwd == "") {
            //提示错误
            weui.alert('密码不能为空');
            return false;
        }

        var reg = /^\w{3,10}$/;
        if (!reg.test(passwd)) {
            weui.alert("请输入3-10个字母或数字或下划线");
            return false;
        }
        if (passwd != confirm) {
            //提示错误
            weui.alert('两次密码输入不一致');
            return false;
        }
        var school = $("#item-school").text();
        if (school == null || school == "") {
            weui.alert('请选择学校');
            return false;
        }

        return true;
    }

    $('#selectSchool').on('click', function () {
        weui.picker(schoolList.data, {
            className: 'custom-classname',
            container: 'body',
            defaultValue: [1, 1, 1],
            onChange: function (result) {
                console.log(result)
            },
            onConfirm: function (result) {
                $('#item-school').text(result[2].label);
            },
            id: 'singleLinePicker'
        });
    });

})